package com.itheima.service.impl;

import com.alibaba.dubbo.config.annotation.Service;
import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.itheima.dao.MenuDao;
import com.itheima.entity.PageResult;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;

@Service(interfaceClass = MenuService.class)
@Transactional
public class MenuServiceImpl implements MenuService {

    //注入MenuDao
    @Autowired
    private MenuDao menuDao;

    //查询所有一级菜单
    public List<Menu> findParentMenus() {
        //调用Dao
        return menuDao.findParentMenus();
    }

    //根据父id查询子菜单
    public List<Menu> findChildrenMenusById(Integer id) {
        //调用Dao
        return menuDao.findChildrenMenusById(id);
    }


    //新增
    @Override
    public void add(Menu menu) {
        menuDao.add(menu);
    }

    //分页查询
    @Override
    public PageResult pageQuery(Integer currentPage, Integer pageSize, String queryString) {
        //分页助手
        PageHelper.startPage(currentPage, pageSize);
        Page<Menu> page = menuDao.selectByCondition(queryString);
        return new PageResult(page.getTotal(), page.getResult());
    }

    //根据id查询
    @Override
    public Menu findById(Integer id) {
        return menuDao.findById(id);
    }

    //编辑
    @Override
    public void edit(Menu menu) {
        menuDao.edit(menu);
    }

    //删除
    @Override
    public void delete(Integer id) {
        //判断当前要删除的检查项是否和检查组有关联
        Long count = menuDao.findCountByMenuId(id);
        if (count > 0) {
            //当前检查项被引用,不能删除
            throw new RuntimeException("当前检查项被引用,不能删除");
        }
        menuDao.deleteById(id);
    }

    @Override
    public List findAll() {
        List<Map<String, Object>> mapList = menuDao.findAll();
        return mapList;
    }

}
